using System;
using System.Messaging;
using System.Reflection;
using log4net;

namespace Patriq.Msmq.Router.Core
{
    public interface IRouteDecider
    {
        string GetDestQueuePathByMessage(Message message);
    }

    public class RouteDecider : IRouteDecider
    {
        private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private readonly IRouterScriptEngineExecutor scriptExecutor;
        public RouteDecider(IRouterScriptEngineExecutor scriptExecutor)
        {
            this.scriptExecutor = scriptExecutor;
            scriptExecutor.Init();
        }

        public string GetDestQueuePathByMessage(Message message)
        {
            try
            {
                var result = scriptExecutor.GetDestQueueByMessage(message);
                return result;
            }
            catch (Exception e)
            {
                Logger.Error(string.Format("error getting destination queue by message [{0}]", message), e);
                throw;
            }
        }
    }
}